package com.itpan.Utils;

import com.itpan.Entity.Customer;
import com.itpan.Entity.Employee;
import com.itpan.Entity.Product;
import com.itpan.Entity.PurchaseOrder;
import com.itpan.Entity.PurchaseOrderDetail;
import com.itpan.common.CustomException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.regex.Pattern;

@Service
public class EntityValidate {

    public boolean isValidateOfCustomer(Customer customer) {
        if (StringUtils.isEmpty(customer.getCustomerName())||customer.getCustomerName()==null) {
//            throw new CustomException("客户名称不能为空");
            return false;
        }
        if (!isValidEmail(customer.getEmail())) {
//            throw new CustomException("邮箱格式不正确");
            return false;
        }
        return true;
    }

    public boolean isValidateOfEmployee(Employee employee) {
        if (StringUtils.isEmpty(employee.getEmployeeName())) {
            throw new CustomException("员工姓名不能为空");
        }
        if (!isValidEmail(employee.getEmployeeEmail())) {
            throw new CustomException("员工邮箱格式不正确");
        }
        // 可以继续添加其他字段的验证逻辑
        return true;
    }

    public boolean isValidateOfProduct(Product product) {
        if (StringUtils.isEmpty(product.getProductName())) {
            throw new CustomException("商品名称不能为空");
        }
        // 可以继续添加其他字段的验证逻辑
        return true;
    }

    public boolean isValidateOfPurchaseOrder(PurchaseOrder order) {

        if (order.getOrderDate() == null || order.getOrderDate().isAfter(LocalDateTime.now())) {
            throw new CustomException("采购时间不合法");
        }
        // 可以继续添加其他字段的验证逻辑
        return true;
    }

    public boolean isValidateOfPurchaseOrderDetail(PurchaseOrderDetail detail) {
        if (detail.getQuantity() <= 0) {
            throw new CustomException("采购数量必须大于0");
        }

        if (detail.getTotalPrice() <= 0) {
            throw new CustomException("商品总价必须大于0");
        }
        // 可以继续添加其他字段的验证逻辑
        return true;
    }

    private boolean isValidEmail(String email) {
        String regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
        return Pattern.matches(regex, email);
    }
}
